#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@IDE     ：PyCharm 
@Author  ：kiway
@Date    ：2024/11/3 9:55 
"""

import requests
from ..config import config


def process_filter():
    """整理合约基础交易规则：最小下单数量、最小下单名义价值等"""

    if config.sandbox_mode:
        res = requests.get(url="https://testnet.binancefuture.com/fapi/v1/exchangeInfo")

    else:
        res = requests.get(url="https://fapi.binance.com/fapi/v1/exchangeInfo")

    data = res.json()
    if "code" in data:
        raise ValueError("error occurred in future symbol get trading_rules ")

    trading_rules = {}

    for info_dict in data["symbols"]:

        # 单笔订单 下单最小数量、最大数量、最小名义价值
        rules = {}

        if info_dict["contractType"] == 'PERPETUAL' and info_dict["quoteAsset"] == "USDT":
            for _filter in info_dict["filters"]:
                if _filter["filterType"] == "MARKET_LOT_SIZE":
                    rules["minQty"] = float(_filter["minQty"])
                    rules["maxQty"] = float(_filter["maxQty"])
                # elif filter["filterType"] == 'MAX_NUM_ORDERS':
                #     rules.append(filter["limit"])
                elif _filter["filterType"] == "MIN_NOTIONAL":
                    rules['min_notional'] = float(_filter['notional'])

            trading_rules[info_dict["baseAsset"] + "/USDT:USDT"] = rules
    return trading_rules
